﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Windows.Resources;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;
using ShareFolder.Resources;
using ShareFolder.ViewModel;

namespace ShareFolder
{
    public partial class MainPage : PhoneApplicationPage
    {  
        private IsolatedStorageFile _isf;

        private string _IsTrial;

        public MainPage()
        {
            InitializeComponent();

            this._isf = IsolatedStorageFile.GetUserStoreForApplication();            
            
            TextBlock textBlock = this.txtVersion;
            string fullName = Assembly.GetExecutingAssembly().FullName;
            char[] chrArray = new char[] { '=' };
            string str = fullName.Split(chrArray)[1];
            char[] chrArray1 = new char[] { ',' };
            textBlock.Text = string.Concat("ShareFolder ", str.Split(chrArray1)[0]);
            if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] != Visibility.Visible)
            {
                this.LayoutRoot.Background = null;
            }
            this.method_1();
            this.method_2();
            
            if (this._IsTrial == "Trial")
            {
                //AdControl adControl = new AdControl()
                //{
                //    Width = 480,
                //    Height = 90,
                //    AppId = "5459"
                //};
                //this.stackConnections.Children.Insert(0, adControl);
                this.btnUpgrade.Visibility = Visibility.Visible;
            }
        }

        private void btnConfig_Click(object sender, RoutedEventArgs e)
        {
            ConnectionViewModel dataContext = (sender as Button).DataContext as ConnectionViewModel;
            App.ViewModel.SelectedConnection = dataContext;
            base.NavigationService.Navigate(new Uri("/EditConnection.xaml", UriKind.Relative));
        }

        private void btnFeedBack_Click(object sender, RoutedEventArgs e)
        {
            EmailComposeTask emailComposeTask = new EmailComposeTask()
            {
                To = "webmaster@komodosoft.net",
                Subject = "ShareFolder feedback"
            };
            emailComposeTask.Show();
        }

        private void btnRate_Click(object sender, RoutedEventArgs e)
        {
            (new MarketplaceReviewTask()).Show();
        }

        private void btnUpgrade_Click(object sender, RoutedEventArgs e)
        {
            (new MarketplaceDetailTask()).Show();
        }

        private void lstConnections_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.lstConnections.SelectedItem != null)
            {
                App.ViewModel.SelectedConnection = this.lstConnections.SelectedItem as ConnectionViewModel;
                base.NavigationService.Navigate(new Uri("/Explorer.xaml", UriKind.Relative));
            }
        }

        

        private void method_1()
        {
            StreamResourceInfo resourceStream = Application.GetResourceStream(new Uri("Help.txt", UriKind.Relative));
            if (resourceStream != null)
            {
                Stream stream = resourceStream.Stream;
                if (stream.CanRead)
                {
                    StreamReader streamReader = new StreamReader(stream);
                    this.lblInstructions.Text = streamReader.ReadToEnd();
                }
            }
        }

        private void method_2()
        {
            StreamResourceInfo resourceStream = Application.GetResourceStream(new Uri("News.txt", UriKind.Relative));
            if (resourceStream != null)
            {
                Stream stream = resourceStream.Stream;
                if (stream.CanRead)
                {
                    StreamReader streamReader = new StreamReader(stream);
                    this.lblNews.Text = streamReader.ReadToEnd();
                }
            }
        }

        private void mnuAdd_Click(object sender, EventArgs e)
        {

            base.NavigationService.Navigate(new Uri("/EditConnection.xaml", UriKind.Relative));
        }

        private void mnuExplore_Click(object sender, EventArgs e)
        {
            base.NavigationService.Navigate(new Uri("/Explorer.xaml?local=yes", UriKind.Relative));
        }

        private void mnuPinToStart_Click(object sender, RoutedEventArgs e)
        {
            //ConnectionViewModel dataContext = (sender as MenuItem).DataContext as ConnectionViewModel;
            //if (ShellTile.get_ActiveTiles().FirstOrDefault<ShellTile>((ShellTile shellTile_0) => shellTile_0.get_NavigationUri().ToString().Contains(string.Concat("ConnectionName=", dataContext.Name.ToUpper()))) == null)
            //{
            //    StandardTileData standardTileDatum = new StandardTileData();
            //    standardTileDatum.set_BackBackgroundImage(null);
            //    standardTileDatum.set_BackgroundImage(new Uri("Background.png", UriKind.Relative));
            //    standardTileDatum.set_BackTitle(null);
            //    standardTileDatum.set_Title(dataContext.Name);
            //    standardTileDatum.set_BackContent(null);
            //    StandardTileData standardTileDatum1 = standardTileDatum;
            //    ShellTile.Create(new Uri(string.Concat("/Explorer.xaml?ConnectionName=", dataContext.Name.ToUpper()), UriKind.Relative), standardTileDatum1);
            //}
        }

        private void mnuSettings_Click(object sender, EventArgs e)
        {
            base.NavigationService.Navigate(new Uri("/Settings.xaml", UriKind.Relative));
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (App.ViewModel.IsDataLoaded)
            {
                base.DataContext = null;
            }
            else
            {
                App.ViewModel.LoadData();
                App.ViewModel.LoadSettings();
            }
            App.ViewModel.SelectedConnection = null;
            base.DataContext = App.ViewModel;
            IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication();
            if (!userStoreForApplication.DirectoryExists("content"))
            {
                userStoreForApplication.CreateDirectory("content");
            }
        }
    }
}